package scau.zxck.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

public class PathUtil {
    private static String rootPath = System.getProperty("user.dir") + "/";

    public static String getAbsolutePath(String path) {
        String filePath = rootPath + path;
        File file = new File(filePath);
        if (!file.exists()) {
            return null;
        }
        return filePath;
    }

    public static InputStream getResourceAsStream(String path) {
        String filePath = rootPath + path;
        File file = new File(filePath);
        if (!file.exists()) {
            return null;
        }
        try {
            return new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
//        return PathUtil.class.getClassLoader().getResourceAsStream(filePath);
    }

    public static URL getResource(String path) {
        System.out.println(rootPath + path);
        String filePath = rootPath + path;
        File file = new File(filePath);
        if (!file.exists()) {
            return null;
        }
        try {
            return file.toURL();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return null;
//        return PathUtil.class.getClassLoader().getResource(filePath);
    }
}
